/*
 * workerManager.cpp
 *
 *  Created on: 2020年9月14日
 *      Author: chencheng
 */

#include "./workerManager.h"

WorkerManager::WorkerManager() {

	/**
	 * 1.第一次使用，文件未创建
	 * 2.文件存在，但是数据被用户清空
	 * 3.文件存在，并且保存职工的所有数据
	 */
	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	//文件不存在
	if (!ifs.is_open()) {
		cout << "文件不存在！" << endl;
		//初始化人数
		this->m_EmpNum = 0;
		//初始化数组指针
		this->m_EmpArray = NULL;
		this->m_FileIsEmpty = true;
		ifs.close();
	} else { //文件存在
		char isFileNull;
		//读取文件一个字符,如果文件为空，则会读取结尾标志
		ifs >> isFileNull;

		if (ifs.eof()) {
			cout << "文件存在，但是为空！" << endl;
			//初始化人数
			this->m_EmpNum = 0;
			//初始化数组指针
			this->m_EmpArray = NULL;
			this->m_FileIsEmpty = true;
			ifs.close();

		} else {
			ifs.close();

			this->m_EmpNum = this->get_EmpNum();
			//cout << "职工人数为：  " << this->m_EmpNum << endl;
			this->m_FileIsEmpty = false;
			this->m_EmpArray = new Worker*[this->m_EmpNum];

			this->init_Empty();

//			for (int i = 0; i < this->m_EmpNum; i++) {
//				cout << "职工号： " << this->m_EmpArray[i]->m_Id << "\t" << "职工姓名： "
//						<< this->m_EmpArray[i]->m_Name << "\t" << "职工部门编号： "
//						<< this->m_EmpArray[i]->m_DeptId << endl;
//			}

		}

	}

}
//判断文件是否为空
bool WorkerManager::isFileEmpty() {
	return this->m_FileIsEmpty;
}

WorkerManager::~WorkerManager() {
	if (this->m_EmpArray != NULL) {
		for (int i = 0; i < this->m_EmpNum; i++) {
			if (this->m_EmpArray[i] != NULL) {
				delete this->m_EmpArray[i];
			}
		}
		this->m_EmpNum = 0;
		this->m_FileIsEmpty = true;
		delete[] this->m_EmpArray;
		this->m_EmpArray = NULL;
		cout << "释放 WorkerManager 中堆中的内存 " << endl;
	}
}

//统计职工人数
int WorkerManager::get_EmpNum() {
	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	int id;
	string name;
	int deptId;

	int num = 0;

	while (ifs >> id && ifs >> name && ifs >> deptId) {
		//记录人数
		num++;
	}

	ifs.close();

	return num;
}

//初始化员工
void WorkerManager::init_Empty() {

	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	int id;
	string name;
	int deptId;
	int index = 0;
	Worker *worker = NULL;
	while (ifs >> id && ifs >> name && ifs >> deptId) {
		//记录人数
		switch (deptId) {
		case 1: //普通职工
			worker = new Employee(id, name, 1);
			break;
		case 2: //经理
			worker = new Manager(id, name, 2);
			break;
		case 3: //老板
			worker = new Boss(id, name, 3);
			break;
		default:
			break;
		}
		this->m_EmpArray[index] = worker;
		index++;
	}

	ifs.close();

}
//显示职工
void WorkerManager::show_Emp() {
	if (this->m_FileIsEmpty) {
		cout << "文件不存在或记录为空！" << endl;
	} else {
		for (int i = 0; i < this->m_EmpNum; i++) {
			this->m_EmpArray[i]->showInfo();
		}
	}
}

//删除职工
void WorkerManager::del_Emp() {
	if (this->m_FileIsEmpty) {
		cout << "文件不存在或者记录为空！" << endl;
	} else {
		//按职工的编号删除
		cout << "请输入想要删除的职工编号：" << endl;
		int id;
		cin >> id;
		if (this->isExist(id)) { //职工存在

			//数据前移
			for (int i = id; i < this->m_EmpNum - 1; i++) {
				this->m_EmpArray[i] = this->m_EmpArray[i + 1];
			}
			this->m_EmpNum--; //更新数组中记录人员个数
			this->save();
		} else {
			cout << "删除失败！未找到该职工！" << endl;
		}

	}

}

//职工是否存在
bool WorkerManager::isExist(int id) {
	if (this->m_FileIsEmpty) {
		return false;
	} else {
		for (int i = 0; i < this->m_EmpNum; i++) {
			if (this->m_EmpArray[i]->m_Id == id) {
				return true;
			}
		}
		return false;
	}
}
//修改职工
void WorkerManager::mod_Emp() {
	if (this->m_FileIsEmpty) {
		cout << "文件不存在或者记录为空！" << endl;
	} else {
		//按职工的编号修改
		cout << "请输入想要修改的职工编号：" << endl;
		int id;
		cin >> id;
		if (this->isExist(id)) { //职工存在
			for (int i = 0; i < this->m_EmpNum; i++) {

				if (this->m_EmpArray[i]->m_Id == id) {
					delete this->m_EmpArray[i];
					//找到职工了
					int newId;
					string newName;
					int newDeptId;

					cout << "查到： " << id << " 号职工，请输入新职工号： " << endl;
					cin >> newId;
					cout << "请输入新的姓名： " << endl;
					cin >> newName;
					cout << "请输入职工新的岗位： " << endl;
					cout << "1、 普通职工 " << endl;
					cout << "2、 经理 " << endl;
					cout << "3、 老板 " << endl;
					cin >> newDeptId;
					Worker *worker = NULL;
					switch (newDeptId) {
					case 1: //普通职工
						worker = new Employee(newId, newName, 1);
						break;
					case 2: //经理
						worker = new Manager(newId, newName, 2);
						break;
					case 3: //老板
						worker = new Boss(newId, newName, 3);
						break;
					default:
						break;
					}
					this->m_EmpArray[i] = worker;

					cout << "修改成功！ " << this->m_EmpArray[i]->m_DeptId << endl;

					//保存到文件
					this->save();

					break;
				}
			}
			//更新数组中记录人员个数
			this->save();
		} else {
			cout << "修改失败！未找到该职工！" << endl;
		}

	}
}

//清空文件
void WorkerManager::clean_File() {
	cout << "确认删除？" << endl;
	cout << "1、确认" << endl;
	cout << "2、返回" << endl;

	int select = 0;
	cin >> select;

	if (select == 1) {
		//打开模式， ios::trunc 如果文件存在则删除文件并重新创建
		ofstream ofs(FILENAME, ios::trunc);
		ofs.close();

		//清除 内存中的所有职工数据
		if (!this->m_FileIsEmpty) {
			if (this->m_EmpArray != NULL) {
				for (int i = 0; i < this->m_EmpNum; i++) {
					if (this->m_EmpArray[i] != NULL) {
						delete this->m_EmpArray[i];
					}
				}
				this->m_EmpNum = 0;
				this->m_FileIsEmpty = true;
				delete[] this->m_EmpArray;
				this->m_EmpArray = NULL;
				cout << "清除成功！ " << endl;
			}

		}

	} else if (select == 2) {
		cout << "退出删除操作！ " << endl;
	}

}
//查找职工
void WorkerManager::find_Emp() {
	if (this->m_FileIsEmpty) {
		cout << "文件不存在或记录为空！ " << endl;

	} else {
		cout << "请输入查找的方式： " << endl;
		cout << "1、按职工编号查找" << endl;
		cout << "2、按姓名查找" << endl;

		int select = 0;
		cin >> select;
		if (select == 1) { //1、按职工编号查找
			int id;
			cout << "请输入查找职工的编号：" << endl;
			cin >> id;
			bool findedEmp = false;
			for (int i = 0; i < this->m_EmpNum; i++) {
				if (this->m_EmpArray[i]->m_Id == id) {
					findedEmp = true;
					cout << "查找成功，该职工信息如下： " << endl;
					this->m_EmpArray[i]->showInfo();
				}
			}

			if (!findedEmp) { //未找到
				cout << "查找失败，查无此人！ " << endl;
			}

		} else if (select == 2) { //2、按姓名查找
			string name;
			cout << "请输入查找职工的姓名：" << endl;
			cin >> name;
			bool findedEmp = false;
			for (int i = 0; i < this->m_EmpNum; i++) {
				if (this->m_EmpArray[i]->m_Name == name) {
					findedEmp = true;
					cout << "查找成功！该职工编号为： " << this->m_EmpArray[i]->m_Id
							<< " 号的信息如下： " << endl;
					this->m_EmpArray[i]->showInfo();
					//break;  全部查找，因为有重名的情况存在！
				}
			}

			if (!findedEmp) { //未找到
				cout << "查找失败，查无此人！ " << endl;
			}

		} else {
			cout << "输入错误：请输入 1 或 2" << endl;
		}

	}
}

//排序职工
void WorkerManager::sort_Emp() {
	if (this->m_FileIsEmpty) {
		cout << "文件不存在或记录为空，无法排序！ " << endl;

	} else {
		cout << "请选择排序方式：" << endl;
		cout << "1、按职工号进行升序" << endl;
		cout << "2、按职工号进行降序" << endl;

		int select = 0;
		cin >> select;

		for (int i = 0; i < this->m_EmpNum; i++) {
			int minOrMax = i;

			for (int j = i + 1; j < this->m_EmpNum; j++) {

				if (select == 1) {
					if (this->m_EmpArray[minOrMax]->m_Id
							> this->m_EmpArray[j]->m_Id) {
						minOrMax = j;
					}
				} else if (select == 2) {
					if (this->m_EmpArray[minOrMax]->m_Id
							< this->m_EmpArray[j]->m_Id) {
						minOrMax = j;
					}
				} else {
					cout << "输入有误！！" << endl;
					return;
				}

			}

			if(i != minOrMax){
				Worker *worker = this->m_EmpArray[i];
				this->m_EmpArray[i] = this->m_EmpArray[minOrMax];
				this->m_EmpArray[minOrMax] = worker;

			}

		}

		cout << "排序成功，排序后结果为：" << endl;
		this->save();
		this->show_Emp();
	}
}

//展示菜单
void WorkerManager::showMenu() {
	cout << "*******************************************************" << endl;
	cout << "***************\t                   欢迎使用职工管理系统\t***************"
			<< endl;
	cout << "***************\t\t0.退出管理程序\t***************" << endl;
	cout << "***************\t\t1.增加职工信息 \t***************" << endl;
	cout << "***************\t\t2.显示职工信息\t***************" << endl;
	cout << "***************\t\t3.删除离职员工\t***************" << endl;
	cout << "***************\t\t4.修改职工信息\t***************" << endl;
	cout << "***************\t\t5.查找职工信息\t***************" << endl;
	cout << "***************\t\t6.按照编号排序\t***************" << endl;
	cout << "***************\t\t7.清空所有文档\t***************" << endl;
	cout << "*******************************************************" << endl;
	cout << endl;
}

//添加员工
void WorkerManager::add_Emp() {
	cout << "请输入添加的职工的数量： " << endl;
	int addNum; //保存用户添加的职工数量
	cin >> addNum;

	if (addNum > 0) {
		int newSize = this->m_EmpNum + addNum;
		Worker **newSpace = new Worker*[newSize];

		//原来的数组有数据
		if (this->m_EmpArray != NULL) {
			for (int i = 0; i < this->m_EmpNum; i++) {
				newSpace[i] = this->m_EmpArray[i];
			}
		}

		//添加新数据
		for (int i = 0; i < addNum; i++) {
			int id; //职工编号
			string name; //职工姓名
			int deptId; //职工部门id

			cout << "请输入第  " << i + 1 << " 个新职工的编码： " << endl;
			cin >> id;
			cout << "请输入第  " << i + 1 << " 个新职工的姓名： " << endl;
			cin >> name;
			cout << "请输入职工的岗位： " << endl;
			cout << "1、 普通职工 " << endl;
			cout << "2、 经理 " << endl;
			cout << "3、 老板 " << endl;
			cin >> deptId;

			Worker *worker = NULL;

			switch (deptId) {
			case 1: //普通职工
				worker = new Employee(id, name, 1);
				break;
			case 2: //经理
				worker = new Manager(id, name, 2);
				break;
			case 3: //老板
				worker = new Boss(id, name, 3);
				break;
			default:
				break;
			}
			newSpace[this->m_EmpNum + i] = worker;
		}
		//释放原有的数组空间
		delete[] this->m_EmpArray;
		//更新新的职工数组，以及人数
		this->m_EmpArray = newSpace;
		this->m_EmpNum = newSize;
		//存在职工
		this->m_FileIsEmpty = false;

		cout << " 成功添加 " << addNum << " 名新职工！" << endl;

		//成功添加后，保存到文件中
		this->save();

	} else {
		cout << "输入数据有误！" << endl;
	}

}

void WorkerManager::save() {
	ofstream ofs;
	ofs.open(FILENAME, ios::out);
	cout << "文件是否打开： " << ofs.is_open() << endl;

	for (int i = 0; i < this->m_EmpNum; i++) {
		ofs << this->m_EmpArray[i]->m_Id << " " << this->m_EmpArray[i]->m_Name
				<< " " << this->m_EmpArray[i]->m_DeptId << endl;
	}

	ofs.close();

}

//退出程序
void WorkerManager::exitSystem() {
	cout << "欢迎下次使用！" << endl;
	exit(0);
}
